package utils;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import model.Combo;
import model.Hero;
import model.HeroCombo;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import android.content.Context;
import android.util.Log;

public class HeroXMLDomParser {

	private Document document;

	public HeroXMLDomParser(Context appContext) throws ParserConfigurationException, IOException, SAXException {
		DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
		domFactory.setNamespaceAware(true);
		domFactory.setIgnoringComments(true);
		domFactory.setIgnoringElementContentWhitespace(true);
		domFactory.setCoalescing(true);
		DocumentBuilder domBuilder = null;

		domBuilder = domFactory.newDocumentBuilder();
		InputStream is = appContext.getAssets().open("combos.xml");
		document = domBuilder.parse(is);
	}

	public ArrayList<HeroCombo> parseXML() {
		ArrayList<HeroCombo> heroComboList = new ArrayList<HeroCombo>();

		Log.i("INFO", "Root element :" + document.getDocumentElement().getNodeName());
		NodeList heroComboNodeList = document.getElementsByTagName("hero-combo");

		Log.i("INFO", "heroComboList size: " + heroComboNodeList.getLength());
		for (int i = 0; i < heroComboNodeList.getLength(); i++) {
			Element heroComboNode = (Element) heroComboNodeList.item(i);
			Log.i("INFO", "heroComboNodeName: " + heroComboNode.getNodeName());
			HeroCombo heroCombo = new HeroCombo();
			Hero mainHero = new Hero();

			NodeList heroList = heroComboNode.getElementsByTagName("main-hero");
			for (int j = 0; j < heroList.getLength(); j++) {
				Element heroNode = (Element) heroList.item(j);
				Log.i("INFO", "heroNodeName: " + heroNode.getNodeName());
				NodeList heroNameNode = heroNode.getElementsByTagName("heroName");
				NodeList heroRoleNode = heroNode.getElementsByTagName("heroRole");

				Element heroNameElement = (Element) heroNameNode.item(0);
				Element heroRoleElement = (Element) heroRoleNode.item(0);

				String mainHeroName = heroNameElement.getChildNodes().item(0).getNodeValue();
				String mainHeroRole = heroRoleElement.getChildNodes().item(0).getNodeValue();

				Log.i("INFO", "heroName= " + mainHeroName);
				Log.i("INFO", "heroRole= " + mainHeroRole);

				mainHero.setHeroName(mainHeroName);
				mainHero.setHeroRole(mainHeroRole);
			}

			List<Combo> listCombos = new ArrayList<Combo>();
			NodeList comboList = heroComboNode.getElementsByTagName("combo");
			for (int j = 0; j < comboList.getLength(); j++) {
				Element comboNode = (Element) comboList.item(j);
				Log.i("INFO", "COMBO");

				NodeList heroNodes = comboNode.getElementsByTagName("hero");
				for (int k = 0; k < heroNodes.getLength(); k++) {
					Element hero = (Element) heroNodes.item(k);

					NodeList nameNode = hero.getElementsByTagName("heroName");
					NodeList commentaryNode = hero.getElementsByTagName("commentary");

					// Log.i("INFO", "nameNode size: " + nameNode.getLength());
					// Log.i("INFO", "commentaryNode size: " +
					// commentaryNode.getLength());

					Element nameElement = (Element) nameNode.item(0);
					Element commentaryElement = (Element) commentaryNode.item(0);

					String heroName = nameElement.getChildNodes().item(0).getNodeValue();
					String commentary = commentaryElement.getChildNodes().item(0).getNodeValue();
					Combo combo = new Combo(heroName, commentary);

					Log.i("INFO", "heroName: " + heroName);
					Log.i("INFO", "commentary: " + commentary);

					listCombos.add(combo);
				}
			}
			heroCombo.setMainHero(mainHero);
			heroCombo.setCombo(listCombos);
			heroComboList.add(heroCombo);

		}

		return heroComboList;
	}
}
